package com.wisely.xxboot.web;

import com.wisely.xxboot.dao.MongoPersonRepository;
import com.wisely.xxboot.domain.MongoLocation;
import com.wisely.xxboot.domain.MongoPerson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: 中视数讯</p>
 * <p>Site: www.digi-zones.com</p>
 *
 * @author shengping
 * @version 1.0
 * @date 2017/7/3
 */
@RestController
public class MongoDataContorller {

    @Autowired
    MongoPersonRepository mongoPersonRepository;

    @RequestMapping("/mongosave") //1 保存
    public MongoPerson save(){
        MongoPerson p = new MongoPerson("aping",28);
        Collection<MongoLocation> locations = new LinkedHashSet<MongoLocation>();
        MongoLocation l1 = new MongoLocation("荆州","2005");
        MongoLocation l2 = new MongoLocation("上海","2005");
        MongoLocation l3 = new MongoLocation("武汉","2006");
        MongoLocation l4 = new MongoLocation("北京","2008");
        MongoLocation l5 = new MongoLocation("天津","2008");
        MongoLocation l6 = new MongoLocation("河北","2008");
        locations.add(l1);
        locations.add(l2);
        locations.add(l3);
        locations.add(l4);
        locations.add(l5);
        locations.add(l6);
        p.setLocations(locations);
        return mongoPersonRepository.save(p);
    }

    @RequestMapping("mongoq1")  //2 方法名查询
    public MongoPerson q1(String name){
        return mongoPersonRepository.findByName(name);
    }
    @RequestMapping("/mongoq2") //3 测试Query查询
    public List<MongoPerson> q2(Integer age){
        return mongoPersonRepository.withQueryFindByAge(age);
    }
}
